﻿using System;
using System.Collections.Generic;
using System.Linq;
using Wizard.WizardControl.Models;
using Wizard.WizardControl.Helpers;
using CoffeeLibrary;

namespace Wizard.CoffeeModel
{

    public class SummaryStepViewModel : WizardStepViewModelBase<CupOfCoffee>
    {
        readonly RegularOptionGroupViewModel<BeanType> _availableBeanTypes;
        readonly RouteOptionGroupViewModel<DrinkSize> _availableDrinkSizes;
        readonly RegularOptionGroupViewModel<Flavorings> _availableFlavorings;
        readonly RegularOptionGroupViewModel<Temperature> _availableTemperatures;

        public SummaryStepViewModel(
            CupOfCoffee c,
            RegularOptionGroupViewModel<BeanType> availableBeanTypes,
            RouteOptionGroupViewModel<DrinkSize> availableDrinkSizes,
            RegularOptionGroupViewModel<Flavorings> availableFlavorings,
            RegularOptionGroupViewModel<Temperature> availableTemperatures )
            : base( c )
        {
            _availableBeanTypes = availableBeanTypes;
            _availableDrinkSizes = availableDrinkSizes;
            _availableFlavorings = availableFlavorings;
            _availableTemperatures = availableTemperatures;
        }

        public string BeanType
        {
            get
            {
                var selectedOption = _availableBeanTypes.OptionModels.FirstOrDefault( option => option.IsSelected );
                return selectedOption == null ? string.Empty : Enum.GetName( typeof(BeanType), selectedOption.GetValue() );
            }
        }

        public override string DisplayName
        {
            get { return "Summary"; }
        }

        public string DrinkSizes
        {
            get
            {
                var selectedOptions = _availableDrinkSizes.OptionModels
                                        .Where( option => option.IsSelected )
                                        .Select( option => Enum.GetName( typeof( DrinkSize ), option.GetValue() ) )
                                        .ToList();  // previous steps enforced selection.
                return selectedOptions.ToDelimitedString( ",", true );
            }
        }

        public string Flavorings
        {
            get
            {
                var flavoringNames = ( from flavoringOption in _availableFlavorings.OptionModels
                                       where flavoringOption.IsSelected
                                       select flavoringOption.DisplayName ).ToList();

                string returnValue = null;

                for ( int i = 0; i < flavoringNames.Count; ++i )
                {
                    returnValue += flavoringNames[i];
                    if ( i < flavoringNames.Count - 1 )
                    {
                        returnValue += ", ";
                    }
                }

                return returnValue ?? "(none)";
            }
        }

        public string Temperature
        {
            get
            {
                var selectedOption = _availableTemperatures.OptionModels.FirstOrDefault( option => option.IsSelected );
                return selectedOption == null ? string.Empty : Enum.GetName( typeof( Temperature ), selectedOption.GetValue() );
            }
        }

        #region Methods

        public override bool IsValid()
        {
            return true;
        }

        #endregion // Methods

    }

}
